package com.example.lin.day9_14_patternlockview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private PatternLockView mPatternLockView;
    private PatternLockView pattern_lock_view_state;
    private TextView tv_state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPatternLockView = findViewById(R.id.pattern_lock_view);
        pattern_lock_view_state = findViewById(R.id.pattern_lock_view_state);
        mPatternLockView.addPatternLockListener(mPatternLockViewListener);

        tv_state = findViewById(R.id.tv_state);
    }

    private String TAG = "MainActivity";


    private String patternResult = null;

    private PatternLockViewListener mPatternLockViewListener = new PatternLockViewListener() {
        @Override
        public void onStarted() {
            Log.d(TAG, "Pattern drawing started");
        }

        @Override
        public void onProgress(List<PatternLockView.Dot> progressPattern) {
            String s = PatternLockUtils.patternToString(mPatternLockView, progressPattern);
        }

        @Override
        public void onComplete(List<PatternLockView.Dot> pattern) {
            //将 经过点 转换成字符串
            String s = PatternLockUtils.patternToString(mPatternLockView, pattern);
            if (s.length() < 4) {
                mPatternLockView.setViewMode(PatternLockView.PatternViewMode.WRONG);
                tv_state.setText("至少需要4个点,请重新绘制!");
                h.sendEmptyMessageDelayed(MSG_WHAT_WRONG, 1000);
                mPatternLockView.setEnabled(false);
            } else {
                if (patternResult != null) {
                    if (patternResult.equals(s)) {
                        h.sendEmptyMessage(MSG_WHAT_COMPLETE);
                    } else {
                        //两次图案不一致
                        tv_state.setText("两次图案不一致!");
                        mPatternLockView.setViewMode(PatternLockView.PatternViewMode.WRONG);
                        h.sendEmptyMessageDelayed(MSG_WHAT_CONFIRM, 1000);
                        mPatternLockView.setEnabled(false);
                    }
                } else {
                    patternResult = s;
                    tv_state.setText("确认手势图案");
                    mPatternLockView.setViewMode(PatternLockView.PatternViewMode.CORRECT);
                    h.sendEmptyMessageDelayed(MSG_WHAT_CONFIRM, 1000);
                    mPatternLockView.setEnabled(false);
                    //静态绘制
                    pattern_lock_view_state.setPattern(PatternLockView.PatternViewMode.CORRECT,pattern);
                }
            }
        }

        @Override
        public void onCleared() {
            Log.d(TAG, "Pattern has been cleared");
        }
    };

    private final static int MSG_WHAT_WRONG = 1;
    private final static int MSG_WHAT_CONFIRM = 2;
    private final static int MSG_WHAT_COMPLETE = 3;

    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_WHAT_WRONG:
                    mPatternLockView.clearPattern();//清空绘制的图案
                    mPatternLockView.setEnabled(true);
                    tv_state.setText("请绘制手势图案");
                    break;
                case MSG_WHAT_CONFIRM:
                    mPatternLockView.clearPattern();//清空绘制的图案
                    mPatternLockView.setEnabled(true);
                    tv_state.setText("确认手势图案");
                    break;
                case MSG_WHAT_COMPLETE:
                    patternResult = null;
                    Toast.makeText(MainActivity.this, "手势密码设置成功!", Toast.LENGTH_SHORT).show();
                    mPatternLockView.clearPattern();//清空绘制的图案
                    break;
            }
            return false;
        }
    });
}
